<%@page import="com.szboanda.platform.common.utils.StringUtils"%>
<%@page import="com.szboanda.platform.v3.util.BeanUtils"%>
<%@page import="com.szboanda.platform.v3.util.DateUtils"%>
<%@ page language="java" import="com.szboanda.platform.v3.util.TWdstore"%>
<%@page language="java" import="com.szboanda.platform.v3.util.dao.JdbcDAO" pageEncoding="utf-8"%>
<%@page import="com.szboanda.platform.v3.util.helper.NamedQueryHelper"%>
<%@page import="com.szboanda.platform.v3.util.helper.QueryHelper"%>
<%@page import="com.szboanda.platform.v3.util.resources.SystemCode"%>
<%@page import="com.szboanda.platform.v3.util.resources.SystemCodesCache"%>
<%@page import="com.szboanda.platform.v3.workflow.definition.IDepartment"%>
<%@page import="com.szboanda.platform.v3.workflow.definition.IUser"%>
<%@page import="com.szboanda.platform.v3.workflow.jdbc.JDBCUsersCache"%>
<%@page import="com.zhuozhengsoft.pageoffice.wordwriter.DataRegion"%>
<%@page import="com.zhuozhengsoft.pageoffice.wordwriter.Font"%>

<%@ page import="org.apache.commons.beanutils.DynaBean"%>
<%@ page import="java.sql.Types"%>
<%@page import="java.util.Date"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Map"%>
<%!
	TWdstore getWdstore(String appid, String modid,String zlx){
		TWdstore wd = null;
		if(StringUtils.isNotEmpty(appid)){
			String sql = "SELECT * FROM T_WDSTORE WHERE APPBH=? AND WDSX=? ORDER BY CJSJ DESC";
			if(StringUtils.isNotEmpty(zlx)){
				sql = "SELECT * FROM T_WDSTORE WHERE APPBH=? AND WDSX=? AND ZLX='"+zlx+"' ORDER BY CJSJ DESC";
			}
			QueryHelper qHelper = new QueryHelper(sql);
			qHelper.addParameter(Types.VARCHAR, appid);
			qHelper.addParameter(Types.VARCHAR, modid);
			DynaBean bean = new JdbcDAO().getSingleRow(null, qHelper);
			if(bean != null){
				wd = BeanUtils.convertDybeanToModel(bean, TWdstore.class);
			}
		}
		return wd;
	}

	public DynaBean getFwJbxx(String xh){
		JdbcDAO dao = new JdbcDAO();
		String sql = "SELECT T.*,wj.MLSY,MAX(wj.scsj) FROM T_OA_GWGL_FWDJJBXX T left join t_platform_wj_wjxx wj on t.xh = wj.ywsjid and zlxdm = 'ZW' WHERE T.XH=#XH# GROUP BY wj.YWSJID ";
		NamedQueryHelper nqh = new NamedQueryHelper(sql);
		nqh.bindParameter("XH", Types.VARCHAR, xh);
		return dao.getSingleRow(null, nqh);
	}

	//根据匹配类型格式化数据
	String regionValPplx(String regionVal, String zd, String pplx){
		String resultVal = "";
		if(StringUtils.isEmpty(regionVal)){
			return resultVal;
		}
		//默认值
		if("MRZ".equals(pplx)){
			resultVal = regionVal;
		}else if("BMBHQBMMC".equals(pplx)){//部门编号取部门名称
			IDepartment dept = JDBCUsersCache.getInstance().getDepartment(regionVal);
			if(dept != null){
				resultVal = dept.getDeptName();
			}
		}else if("YHBHQYHMC".equals(pplx)){//用户编号取用户名称
			IUser user = JDBCUsersCache.getInstance().getUser(regionVal);
			if(user != null){
				resultVal = user.getUserName();
			}
		}else if("RQZHDXRQ".equals(pplx)){//日期转换大写日期
			Date rq = DateUtils.parseDate(regionVal, "yyyy-MM-dd");
			resultVal = DateUtils.formatChineseDate(rq);
		}else if("RQZH".equals(pplx)){//日期转换（2015年1月1日）
			Date rq = DateUtils.parseDate(regionVal, "yyyy-MM-dd");
			resultVal = DateUtils.simpleDateFormat(rq, "yyyy年M月d日");
		}else if("RQZH1".equals(pplx)){//日期转换（2015年01月01日）
			Date rq = DateUtils.parseDate(regionVal, "yyyy-MM-dd");
			resultVal = DateUtils.simpleDateFormat(rq, "yyyy年MM月dd日");
		}else if("RQZH2".equals(pplx)){//日期转换（2015-01-01）
			Date rq = DateUtils.parseDate(regionVal, "yyyy-MM-dd");
			resultVal = DateUtils.simpleDateFormat(rq, "yyyy-MM-dd");
		}else if("GGDMPP".equals(pplx)){//从公共代码值表取代码内容（字段名和代码集名称一致才有效）
			Map<String,List<SystemCode>> map = SystemCodesCache.getInstance().getAllCahe();
			List<SystemCode> zdValList = map.get(zd);
			for(SystemCode code : zdValList){
				if(code.getCodeId().equals(regionVal)){
					resultVal = code.getValue();
				}
			}
		}
		return resultVal;
	}

	public DynaBean getTHRQ(String bwbh){
		JdbcDAO dao = new JdbcDAO();
		String sql = "SELECT top(1)* FROM T_WORKFLOW_LSBZ WHERE LCSLBH = #BWBH# AND BZDYBH IN ( '6ee1e5f3-333e-41c6-bacf-bba318d9dd27','ef20bffd-e59b-4de8-8cc7-f7900b3bdcbe') ORDER BY CJSJ DESC";
		NamedQueryHelper nqh = new NamedQueryHelper(sql);
		nqh.bindParameter("BWBH", Types.VARCHAR, bwbh);
		return dao.getSingleRow(null, nqh);
	}

	public DynaBean getWcJbxx(String xh){
		JdbcDAO dao = new JdbcDAO();
		String sql = "SELECT T.*,(SELECT distinct WDMC = stuff(( select ', '+WDMC FROM T_WDSTORE wd WHERE wd.APPBH = T.XH AND wd.WDSX = 'WCFJ' AND WDMC = wd.WDMC for xml path('')),1,1,'') from T_WDSTORE) FJ FROM T_OA_RSGL_WCSQ T WHERE T.XH=#XH#";
		NamedQueryHelper nqh = new NamedQueryHelper(sql);
		nqh.bindParameter("XH", Types.VARCHAR, xh);
		return dao.getSingleRow(null, nqh);
	}
/*

	//在线起草公文模板绑定书签专用
	WordDocument bindingDataRegion(String jsonData){
		WordDocument doc = new WordDocument();
		JSONObject jsObj = JSONObject.fromObject(jsonData);
		boolean isZjbd = true;
		String gwlxxh = (String)jsObj.get("PO_XTXH");
		if(StringUtils.isNotEmpty(gwlxxh)){
			//若是公文模板则去T_OA_GWGL_MBSQDYZD表中查找书签与字段对应关系
			List<DynaBean> fieldList = queryMbSqdyzd(gwlxxh,"GWMB");
			if(!BeanUtils.emptyCollection(fieldList)){//如果绑定关系数据存在
				for(DynaBean bean : fieldList){
					String zd = BeanUtils.getString(bean,"ZD");//字段
					String sq = BeanUtils.getString(bean,"SQ");//书签
					String pplx = BeanUtils.getString(bean,"PPLX");//匹配类型
					Calendar cal = Calendar.getInstance();
					String regionVal = "";
					if("YYYY".equals(zd)){
						regionVal = DateUtils.simpleDateFormat(cal.getTime(), "yyyy");
					}else if("MM".equals(zd)){
						regionVal = DateUtils.simpleDateFormat(cal.getTime(), "MM");
					}else if("DD".equals(zd)){
						regionVal = DateUtils.simpleDateFormat(cal.getTime(), "dd");
					}else{
						regionVal = regionValPplx((String)jsObj.get("PO_"+zd), zd, pplx);
					}
					if(StringUtils.isNotEmpty(regionVal)){
						doc.openDataRegion(sq).setValue(regionVal);
					}
				}
				isZjbd = false;//设为非直接绑定模式
			}
		}
		//非公文模板类型 或公文模板没有设置书签与字段对应关系的情况下-->直接将数据绑定对应书签
		if(isZjbd){
			Iterator<String> iter = jsObj.keys();
			while(iter.hasNext()){
				String key = iter.next();
				doc.openDataRegion(key).setValue((String)jsObj.get(key));
			}
		}
		return doc;
	}
*/

	//查询模板书签与字段对应关系数据
	List<DynaBean> queryMbSqdyzd(String gwlxxh,String fl){
		NamedQueryHelper nqh = new NamedQueryHelper("SELECT * FROM T_OA_GWGL_MBSQDYZD WHERE XTXH=#XTXH# AND FL=#FL# ");
		nqh.bindParameter("XTXH", Types.VARCHAR, gwlxxh);
		nqh.bindParameter("FL", Types.VARCHAR, fl);
		return new JdbcDAO().getListValue(null, nqh);
	}
%>
